package edu.byu.isys413.group1E;

import edu.byu.isys413.group1E.data.*;
import java.sql.SQLException;
import java.util.ArrayList;

public class ConversionHandler {
    
    public static void main(String args[]) throws Exception {
        
    }
    
    public RevenueSourceBO addConversion(int qty, String ConversionServID) throws Exception {
        
        //Create DAOs
        TransLineDAO txDAO                =      TransLineDAO.getInstance();
        ConversionDAO cDAO             =       new ConversionDAO();
        ConversionServDAO csDAO    =       new ConversionServDAO();
        DiscountDAO dDAO                 =       new DiscountDAO();
        
        //Aquire BOs
        TransLineBO      txBO             =       txDAO.create();
        
        //New Order
        ConversionBO cBO                =      (ConversionBO)cDAO.create();
        
        //How Many Photos to Convert?
        cBO.setQuantity(qty);
        
        //How would you like it converted?
        ConversionServBO csBO        =       (ConversionServBO)csDAO.read(ConversionServID);
        
        //Lets see if we can find a discount for that!
        DiscountBO dBO                    =       (DiscountBO)dDAO.getbyConvServ(csBO.getId());
        
        //Connect Everything
        cBO.setConversionServiceID(csBO.getId());
        txBO.setRevSrc(cBO);
        
        //Calculate
        int fullpriceitems = qty;
        int discountitems = 0;
        double percent = 0;
        if (dBO != null)
        {
            fullpriceitems = qty - dBO.getQuantity();
            discountitems = dBO.getQuantity();
            percent = dBO.getPercent();
        }
        double price = (fullpriceitems * csBO.getPrice()) +  ((discountitems * csBO.getPrice())*percent);
        cBO.setPrice(price);
        
        //Save Everything
        cDAO.save(cBO);
        
        //ReturnBO
        return cBO;
    }
    
    public ArrayList<BusinessObject> listConvServ() throws DataException, SQLException, ConnectionPoolException {
        ConversionServDAO csDAO    =       new ConversionServDAO();
        return csDAO.readAll();
    }
    
    public void markReady(String id) throws DataException, ConnectionPoolException, SQLException {
        
        ConversionDAO poDAO = (ConversionDAO)ConversionDAO.getInstance();
        ConversionBO poBO = (ConversionBO)poDAO.read(id);
        poBO.setStatus("Ready");
        poDAO.save(poBO);
        
        
    }
    
    public void addService(double price, String description) throws Exception {
        ConversionServDAO csDAO    =       new ConversionServDAO();
        ConversionServBO csBO        =       (ConversionServBO)csDAO.create();
        csBO.setPrice(price);
        csBO.setType(description);
        csDAO.save(csBO);
    }
    
    public BusinessObject findDiscount(int index) throws DataException, SQLException, ConnectionPoolException {
        String cID = this.listConvServ().get(index).getId();
        DiscountDAO dDAO    =       new DiscountDAO();
        return dDAO.getbyConvServ(cID);
    }
    
    public void addDiscount(int qty, double percent, String csID) throws Exception {
        DiscountDAO dDAO    =       new DiscountDAO();
        DiscountBO dBO = (DiscountBO)dDAO.create();
        dBO.setQuantity(qty);
        dBO.setPercent(percent);
        dBO.setConvSerId(csID);
        dDAO.save(dBO);
    }
    
    
    
    public String[] getCSDesc() throws DataException, SQLException, ConnectionPoolException {
        ArrayList<BusinessObject> psList = this.listConvServ();
        int size = psList.size();
        int i = 0;
        String[] psArray = new String[size];
        while (i < size) {
            ConversionServBO psBO = (ConversionServBO)psList.get(i);
            double price = psBO.getPrice();
            psArray[i] = psBO.getType()+" ($"+price+"0)";
            i++;
        }
   return psArray;
    }
}
